const { createWebAPIRequest } = require('../../util/util')
module.exports = [
  {
    /**
    * @api {get} /music/comment/music [评论歌曲列表]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 调用此接口,传入音乐id和limit参数,可获得该音乐的所有评论(不需要登录)
    * @apiParam {Number} id id=186016
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/comment/music',
    request: (req, res) => {
      const rid = req.query.id
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        rid: rid,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/resource/comments/R_SO_4_${rid}/?csrf_token=`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send(err.message)
      )
    }
  }, {
    /**
    * @api {get} /music/comment/mv [mv评论]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 调用此接口 , 传入音乐 id 和 limit 参数 , 可获得该 mv 的所有评论 ( 不需要 登录 )
    * @apiParam {Number} id  id
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/comment/mv',
    request: (req, res) => {
      const rid = req.query.id
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        rid: rid,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/resource/comments/R_MV_5_${rid}/?csrf_token=`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/comment/album [专辑评论]
    * @apiGroup Music
    * @apiPermission none
    * @apiDescription 调用此接口 , 传入音乐 id 和 limit 参数 , 可获得该专辑的所有评论 ( 不需要 登录 )
    * @apiParam {Number} id  id
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/comment/album',
    request: (req, res) => {
      const rid = req.query.id
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        rid: rid,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/resource/comments/R_AL_3_${rid}/?csrf_token=`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/comment/playlist [评论歌单列表]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} id  id
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/comment/playlist',
    request: (req, res) => {
      const rid = req.query.id
      const cookie = req.get('Cookie') ? req.get('Cookie') : ''
      const data = {
        offset: req.query.offset || 0,
        rid: rid,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/resource/comments/A_PL_0_${rid}/?csrf_token=`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/comment/like [评论like]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} id  歌曲 id
    * @apiParam {Number} cid 评论 cid
    */
    url: '/music/comment/like',
    request: (req, res) => {
      const cookie = req.get('Cookie') || ''
      // 评论 cid; 歌曲 id
      const { cid, id } = req.query

      const typeMap = {
        0: 'R_SO_4_', // 歌曲
        1: 'R_MV_5_', // mv
        2: 'A_PL_0_', // 歌单
        3: 'R_AL_3_', // 专辑
        4: 'A_DJ_1_' // 电台
      }
      const type = typeMap[req.query.type]
      const data = {
        threadId: `${type}${id}`,
        commentId: cid,
        csrf_token: ''
      }
      const action = req.query.t === 1 ? 'like' : 'unlike'

      const url = `/weapi/v1/comment/${action}`
      createWebAPIRequest(
        'music.163.com',
        url,
        'POST',
        data,
        cookie,
        musicReq => res.send(musicReq),
        err => res.status(500).send({err})
      )
    }
  }, {
    /**
    * @api {get} /music/comment/dj [评论dj广播]
    * @apiGroup Music
    * @apiPermission none
    * @apiParam {Number} id  id
    * @apiParam {Number} [offset=0] 页
    * @apiParam {Number} [limit=20]  每页数量
    */
    url: '/music/comment/dj',
    request: (req, res) => {
      const rid = req.query.id
      const cookie = req.get('Cookie') || ''
      const data = {
        offset: req.query.offset || 0,
        rid: rid,
        limit: req.query.limit || 20,
        csrf_token: ''
      }
      createWebAPIRequest(
        'music.163.com',
        `/weapi/v1/resource/comments/A_DJ_1_${rid}/?csrf_token=`,
        'POST',
        data,
        cookie,
        musicReq => {
          res.send(musicReq)
        },
        err => res.status(500).send({err})
      )
    }
  }
]
